/*
 * @lc app=leetcode.cn id=112 lang=cpp
 *
 * [112] 路径总和
 */

// @lc code=start
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    bool hasPathSum(TreeNode* root, int sum) {
        return dfs(root, 0, sum);
    }
    bool dfs(TreeNode* root, int curr_sum, const int& sum)
    {
        if(root == NULL)
        {
            return false;
        }
        curr_sum += root->val;

        if(curr_sum == sum)
        {
            if(root->left == NULL && root->right == NULL)
                return true;
        }
        bool l_flag = dfs(root->left, curr_sum, sum);
        bool r_flag = dfs(root->right, curr_sum, sum);
        return (l_flag||r_flag );
    }
};
// @lc code=end

